package ATMproject;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class SqlUtil {
    public Connection getConnection() {
        Connection con = null;
        // 1. 加载驱动
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            // 2. 创建连接
            con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/atm?userSSL=true&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT",
                    "root", "Doksql721.");
//            System.out.println(con);

        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
        return con;
    }

    /**
     * 增加，删除，修改数据
     * @param
     */
    public void insert(String name, String password) {
        //1.创建连接
        Connection con = getConnection();
        Statement statement = null;
        try {
            //2.获取执行SQL语句对象
            statement = con.createStatement();
            String sql = "update t_bank set b_userpsw='"+ password +"' where b_username='" + name + "'";
            //3.执行SQL语句
            int row = statement.executeUpdate(sql);
            System.out.println("影响的行数：" + row);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //4.关闭连接
            close(con, statement);
        }
    }


    //关闭添加
    public void close(Connection con, Statement statement) {
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (con != null) {
            try {
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    //关闭查询
    public void close(Connection con, Statement statement, ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (con != null) {
            try {
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    /**
     * 查询
     */

    public List queryMethod() {
        //1.创建连接
        Connection con = getConnection();
        Statement statement = null;
        ResultSet rs = null;

        List userList = new ArrayList();
        try {
            //2.获取执行SQL语句的对象
            statement = con.createStatement();
            String sql = "select * from t_student";
            //3.执行SQL并返回结果集
            rs = statement.executeQuery(sql);
            while (rs.next()) {
                User user = new User();
                user.setId(rs.getInt("b_id"));
                user.setAccount(rs.getString("b_username"));
                user.setPassword(rs.getString("b_userpsw"));
                user.setMoney(rs.getDouble("b_money"));
                userList.add(user);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con, statement, rs);
        }
        return userList;
    }
}
